/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.user.entity.UserApiConfig;
import com.pig4cloud.pigx.user.entity.UserApiLockDuration;
import com.pig4cloud.pigx.user.mapper.UserApiConfigMapper;
import com.pig4cloud.pigx.user.mapper.UserApiLockDurationMapper;
import com.pig4cloud.pigx.user.mapstruct.UserApiConfigVOStruct;
import com.pig4cloud.pigx.user.service.UserApiConfigService;
import com.pig4cloud.pigx.user.util.UserUtil;
import com.pig4cloud.pigx.user.vo.UserApiConfigDetailVO;
import com.pig4cloud.pigx.user.vo.UserApiConfigVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * api配置表
 *
 * @since 2022-10-31 15:36:10
 */
@Service
@AllArgsConstructor
public class UserApiConfigServiceImpl extends ServiceImpl<UserApiConfigMapper, UserApiConfig> implements UserApiConfigService {

	private final UserApiLockDurationMapper userApiLockDurationMapper;

	private final UserUtil userUtil;

	/**
	 * 查询详情
	 *
	 * @return {@link UserApiConfigDetailVO}
	 * @since 2022/10/31 19:47
	 */
	@Override
	public UserApiConfigDetailVO detail() {
		UserApiConfigVO vo = userUtil.getApiConfig();
		UserApiConfigDetailVO detailVo = UserApiConfigVOStruct.INSTANCE.vo2DetailVO(vo);

		// 查询Api封禁时长配置分页数据
		Page<UserApiLockDuration> page = new Page<>(1, 10);
		QueryWrapper<UserApiLockDuration> queryLockDuration = new QueryWrapper<>();
		queryLockDuration.lambda()
				.orderByDesc(UserApiLockDuration::getLockNum);

		page = userApiLockDurationMapper.selectPage(page, queryLockDuration);
		detailVo.setLockDurationPage(page);

		return detailVo;
	}

	/**
	 * 修改
	 *
	 * @param param 参数
	 * @since 2022/10/31 20:28
	 */
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void update(UserApiConfigVO param) {
		// 将 UserApiConfigVO 转换为 List
		List<UserApiConfig> list = userUtil.apiConfigVO2List(param);

		QueryWrapper<UserApiConfig> query = new QueryWrapper<>();
		// 查询旧记录数量
		Long count = baseMapper.selectCount(query);

		// 删除所有旧记录
		if (baseMapper.delete(query) != count) {
			throw new RuntimeException("操作失败");
		}

		// 批量新增
		if (baseMapper.insertBatch(list) != list.size()) {
			throw new RuntimeException("操作失败");
		}

		// 更新缓存
		userUtil.flushRedisApiConfig(param);
	}
}
